// subPages/bodyFat/dataContrast/dataContrast.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.item != 'undefined' && options.item != null)
    {
      this.setData({
        isNewUser: 2,
        userInfo: JSON.parse(options.item)
      })
      wx.setNavigationBarTitle({
        title: this.data.userInfo.nickName + '的数据对比' //页面标题为路由参数
      })
    }else{
      this.setData({
        isNewUser: 1,
        userInfo: app.globalData.userInfo,
      })
    }
    this.getDataContrastList(options);
  },

  getDataContrastList(options){
    var that = this
    let idList = JSON.parse(options.idList)
    wx.request({
        url: app.globalData.apiUrl +'/checkdb',
        data: {
          id: idList.id1,
          fid : idList.id2
        },
        method: 'get',
        header: {
          'content-type': 'application/json' //默认值
        },
        success: function (res) {
          // console.log(res)
          if(res.statusCode == '200'){
            that.setData({
              dataRecordList : res.data,
              time1: idList.time1,
              time2: idList.time2
            })
            that.setType(that.data.dataRecordList);
            that.setUtil(that.data.dataRecordList);
          }
        },
        fail: function (res) {
          console.log("失败");
        }
    })
  },

  //设置类型
  setType:function(dataRecordList){
    var that = this
    for(var i in dataRecordList){
      if(dataRecordList[i].title == '体重')
      {
        that.setData({
          type1: dataRecordList[i].type,
          type2: dataRecordList[i].ftype
        })
      }
    }
  },

  //设置单位
  setUtil:function(dataRecordList){
    var that = this
    for(var i in dataRecordList){
      if(dataRecordList[i].title == '体重')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: 'kg',
        })
      }
      if(dataRecordList[i].title == 'BMI')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: '',
        })
      }
      if(dataRecordList[i].title == '体脂率')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: '%',
        })
      }
      if(dataRecordList[i].title == '肌肉量')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: 'kg',
        })
      }
      if(dataRecordList[i].title == '体年龄')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: '岁',
        })
      }
      if(dataRecordList[i].title == '水分率')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: '%',
        })
      }
      if(dataRecordList[i].title == '内脏脂肪')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: '',
        })
      }
      if(dataRecordList[i].title == '骨量')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: 'kg',
        })
      }
      if(dataRecordList[i].title == '基础代谢')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: 'kcal',
        })
      }
      if(dataRecordList[i].title == '蛋白质率')
      {
        var util = "dataRecordList[" + i + "].util"
        that.setData({
          [util]: '%',
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN小程序',
      path: 'pages/index/index?puid=' + app.globalData.userId,
      success: function(res) {
          res.shareTickets 
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})